package gw.com.android.ui.bulletin;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import gw.com.android.app.AppContances;
import gw.com.android.app.GTConfig;
import gw.com.android.utils.NotificationUtil;
import gw.com.android.utils.PackStatis;

/**
 * @author jett
 * @since 2018-07-26.
 */
public class NotifyCentreAdapter extends RecyclerView.Adapter {

    private static final int TYPE_OPEN_SYS = 1;

    private final Activity activity;
    private boolean showOpenSys;
    private List<NotifyCategoryData> mDatas;

    NotifyCentreAdapter(Activity activity, boolean showOpenSys) {
        this.activity = activity;
        this.showOpenSys = showOpenSys;
    }

    public void setData(List<NotifyCategoryData> list) {
        mDatas = list;
        notifyDataSetChanged();
    }

    public void setShowOpenSys(boolean showOpenSys) {
        this.showOpenSys = showOpenSys;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_OPEN_SYS) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_notify_open_sys, parent, false);
            return new OpenSysHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_notify_centre, parent, false);
            return new NoticeCentreHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        NotifyCategoryData data = getItem(position);
        if (data == null) {
            return;
        }
        if (!(holder instanceof NoticeCentreHolder)) {
            return;
        }
        NoticeCentreHolder mHolder = (NoticeCentreHolder) holder;
        mHolder.data = data;
        mHolder.ivIcon.setImageResource(data.icon);
        mHolder.tvTitle.setText(data.name);
        if (data.notice != null) {
            mHolder.tvContent.setText(data.notice.title);
            mHolder.tvDate.setText(formatDate(data.notice.ctime));
        } else {
            mHolder.tvContent.setText(R.string.push_empty);
            mHolder.tvDate.setText("");
        }
        if (data.unreadCount <= 0) {
            mHolder.tvRedPoint.setVisibility(View.GONE);
        } else {
            mHolder.tvRedPoint.setVisibility(View.VISIBLE);
            mHolder.tvRedPoint.setText(formatUnreadCount(data.unreadCount));
        }
    }

    private NotifyCategoryData getItem(int position) {
        if (showOpenSys) {
            position--;
        }
        if (mDatas != null && position >= 0 && position < mDatas.size()) {
            return mDatas.get(position);
        }
        return null;
    }

    @Override
    public int getItemCount() {
        int header = showOpenSys ? 1 : 0;
        if (mDatas != null) {
            return mDatas.size() + header;
        }
        return header;
    }

    @Override
    public int getItemViewType(int position) {
        if (showOpenSys && position == 0) {
            return TYPE_OPEN_SYS;
        }
        return super.getItemViewType(position);
    }

    private String formatDate(long dateTime) {
        if (dateTime <= 0) {
            return "";
        }
        dateTime = dateTime * 1000;
        if (DateUtils.isToday(dateTime)) {
            return new SimpleDateFormat("HH:mm", Locale.getDefault()).format(new Date(dateTime));
        } else {
            return new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault()).format(new Date(dateTime));
        }
    }

    private String formatUnreadCount(int count) {
        if (count <= 0) {
            return "0";
        } else if (count < 100) {
            return String.valueOf(count);
        } else {
            return "99+";
        }
    }

    class NoticeCentreHolder extends RecyclerView.ViewHolder {

        NotifyCategoryData data;

        ImageView ivIcon;
        TextView tvRedPoint;
        TextView tvTitle;
        TextView tvContent;
        TextView tvDate;

        NoticeCentreHolder(View itemView) {
            super(itemView);
            ivIcon = (ImageView) itemView.findViewById(R.id.notice_centre_icon);
            tvRedPoint = (TextView) itemView.findViewById(R.id.notice_centre_red_point);
            tvTitle = (TextView) itemView.findViewById(R.id.notice_centre_title);
            tvContent = (TextView) itemView.findViewById(R.id.notice_centre_content);
            tvDate = (TextView) itemView.findViewById(R.id.notice_centre_date);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (data != null) {
                        onCollect(data.name);
                        Intent intent = new Intent(activity, NotifyCategoryActivity.class);
                        intent.putExtra("typeId", data.typeId);
                        intent.putExtra("typeName", data.name);
                        activity.startActivity(intent);
                    }
                }
            });
        }

    }

    private void onCollect(String name) {
        String action;
        if ("行情提醒".equals(name)) {
            action = "clickMarket";
        } else if ("交易策略".equals(name)) {
            action = "clickStrategy";
        } else if ("系统通知".equals(name)) {
            action = "clickSystem";
        } else if ("分析师订阅".equals(name)) {
            action = "clickTeachers";
        } else if ("互动提醒".equals(name)) {
            action = "clickInteraction";
        } else {
            return;
        }
        PackStatis.getToService(action, "Me", null, null);
    }

    class OpenSysHolder extends RecyclerView.ViewHolder {

        OpenSysHolder(View itemView) {
            super(itemView);
            ImageView ivOpenSysClose = itemView.findViewById(R.id.notice_centre_opensys_close);
            TextView tvOpenSysOpen = itemView.findViewById(R.id.notice_centre_opensys_open);
            ivOpenSysClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PackStatis.getToService("clickClose", "Me", null, null);
                    GTConfig.instance().setBooleanValue("notify_open_sys_showed", true);
                    setShowOpenSys(false);
                    notifyDataSetChanged();
                }
            });
            tvOpenSysOpen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PackStatis.getToService("clickOpen", "Me", null, null);
                    NotificationUtil.toNotificationSetting(activity, AppContances.REQUEST_SYSTEM_PUSH_CODE);
                }
            });
        }

    }

}
